package com.fmatos.amazingwallpapers.image.database.orm;

import java.util.concurrent.atomic.AtomicInteger;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.android.apptools.OpenHelperManager;

/**
 * Keeps a reference counter for the database helper instance. 
 * This is supposed to be called from activities or services only, since they're the 
 * only ones that know enough about lifecycles. 
 *   
 * @author fdematos
 *
 */
public class DatabaseManager {
	
	
	private static final String TAG = DatabaseManager.class.getSimpleName();
	
	private static AtomicInteger referenceCounter = new AtomicInteger();

	private static DatabaseHelper databaseHelper;
	
	
	public static DatabaseHelper getDatabaseHelper() {
		Log.i(TAG,"Database counter return instance, counter = " + referenceCounter);
		return databaseHelper;
	}
	
	/**
	 * Opens database if closed, and increment instance counter
	 */
	public synchronized static boolean acquireDatabase(Context context) {
		
		Log.e(TAG,"Database counter increase = " + referenceCounter + " reference = " + referenceCounter.hashCode());
		if ( 0 == referenceCounter.getAndIncrement() ) {
			databaseHelper = OpenHelperManager.getHelper(context.getApplicationContext(), DatabaseHelper.class);
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Decreases increment counter and closes database if no one is using it
	 */
	public synchronized static boolean releaseDatabase() {
		Log.e(TAG,"Database counter decrease = " + referenceCounter);
		
		if ( 0 == referenceCounter.decrementAndGet() ) {
			OpenHelperManager.releaseHelper();
			databaseHelper.close();
			databaseHelper = null;
			return true;
		} else {
			if ( referenceCounter.intValue() < 0) {
				Log.e(TAG,"Somethings very wrong, your counter is negative  = " + referenceCounter.intValue());
			}
			return false;
		}	
	}
	
}

//
//import android.content.Context;
//
//public class DatabaseManager {
//
//	static private DatabaseManager instance;
//
//	static public void init(Context ctx) {
//		if (null==instance) {
//			instance = new DatabaseManager(ctx);
//		}
//	}
//
//	static public DatabaseManager getInstance() {
//		return instance;
//	}
//
//	private DatabaseHelper helper;
//	private DatabaseManager(Context ctx) {
//		helper = new DatabaseHelper(ctx);
//	}
//
//	private DatabaseHelper getHelper() {
//		return helper;
//	}
//
////	public List<WishList> getAllWishLists() {
////		List<WishList> wishLists = null;
////		try {
////			wishLists = getHelper().getWishListDao().queryForAll();
////		} catch (SQLException e) {
////			e.printStackTrace();
////		}
////		return wishLists;
////	}
//
////	public void addAlbum(WishList l) {
////		try {
////			getHelper().getWishListDao().create(l);
////		} catch (SQLException e) {
////			e.printStackTrace();
////		}
////	}
////
////	public WishList getWishListWithId(int wishListId) {
////		WishList wishList = null;
////		try {
////			wishList = getHelper().getWishListDao().queryForId(wishListId);
////		} catch (SQLException e) {
////			e.printStackTrace();
////		}
////		return wishList;
////	}
////	
////
////	public WishItem getWishItemWithId(int wishItemId) {
////		WishItem wishList = null;
////		try {
////			wishList = getHelper().getWishItemDao().queryForId(wishItemId);
////		} catch (SQLException e) {
////			e.printStackTrace();
////		}
////		return wishList;
////	}
////
////	public WishItem newWishItem() {
////		WishItem wishItem = new WishItem();
////		try {
////			getHelper().getWishItemDao().create(wishItem);
////		} catch (SQLException e) {
////			e.printStackTrace();
////		}
////		return wishItem;
////	}
////
////	public void deleteWishList(WishList wishList) {
////		try {
////			getHelper().getWishListDao().delete(wishList);
////		} catch (SQLException e) {
////			e.printStackTrace();
////		}
////	}
//
////	public void deleteWishItem(WishItem wishItem) {
////		try {
////			getHelper().getWishItemDao().delete(wishItem);
////		} catch (SQLException e) {
////			e.printStackTrace();
////		}		
////	}
////
////	public void updateWishItem(WishItem item) {
////		try {
////			getHelper().getWishItemDao().update(item);
////		} catch (SQLException e) {
////			e.printStackTrace();
////		}
////	}
////
////	public void refreshWishList(WishList wishList) {
////		try {
////			getHelper().getWishListDao().refresh(wishList);
////		} catch (SQLException e) {
////			e.printStackTrace();
////		}
////	}
////
////	public void updateWishList(WishList wishList) {
////		try {
////			getHelper().getWishListDao().update(wishList);
////		} catch (SQLException e) {
////			e.printStackTrace();
////		}
////	}
//
//
//}